// TypeScript file
class startView extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.width = 750;
        this.height = 1334;
        this.init();
    }

    private image: egret.Bitmap;

    private init() {
        this.initButton("倒计时", 20, 1000, this.text_1Init);
        this.initButton("定时器", 200, 1000, this.text_2Init);
        this.initButton("白鹭定时器", 400, 1000, this.text_3Init);
        this.initButton("心跳", 20, 1100, this.text_4Init);
        this.initButton("帧事件", 200, 1100, this.text_5Init);
    }

    private text_1Init() {
        let time = new Date().getTime();
        egret.setTimeout(() => {
            let number = new Date().getTime() - time;
            console.log("这是一个倒计时:setTimeout", number)
        }, this, 1000)
    }

    private text_2Init() {
        let index = 0;
        let time = new Date().getTime();
        egret.setInterval(() => {
            index++;
            let number = new Date().getTime() - time;
            time = new Date().getTime();
            console.log("这是一个定时器:setInterval", index, number)
        }, this, 1000);
    }

    public index: number = 0;
    public time;
    private text_3Init() {
        this.index = 0;
        this.time = new Date().getTime();
        //创建一个计时器对象
        var timer:egret.Timer = new egret.Timer(1000,5);
        //注册事件侦听器
        timer.addEventListener(egret.TimerEvent.TIMER,this.timerFunc,this);
        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.timerComFunc,this);
        //开始计时
        timer.start();
    }

    private timerFunc() {
        this.index += 1;
        let number = new Date().getTime() - this.time;
        this.time = new Date().getTime();
        console.log("计时");
        console.log("这是一个白鹭定时器:Timer", this.index, number);
    }
    private timerComFunc() {
        console.log("计时结束");
        console.log("这是一个白鹭定时器:Timer", this.index);
    }

    private text_4Init() {
        this.index = 0;
        this.time = new Date().getTime();
         egret.startTick(this.onTicks,this);
    }
    private onTicks():boolean {
        this.index += 1;
        let number = new Date().getTime() - this.time;
        this.time = new Date().getTime();
        console.log("计时");
        console.log("这是一个白鹭心跳:Ticker:", this.index, number);
        return false;
    }

    private text_5Init() {
        this.index = 0;
        this.time = new Date().getTime();
        this.addEventListener(egret.Event.ENTER_FRAME,this.onFrame,this);
    }

    private onFrame():boolean {
        this.index += 1;
        let number = new Date().getTime() - this.time;
        this.time = new Date().getTime();
        console.log("计时");
        console.log("这是一个白鹭帧事件ENTER_FRAME:", this.index, number);
        return false;
    }



    //通用按钮
    private initButton(textContent, x, y, callback) {
        var text: egret.TextField = new egret.TextField();
        text.text = textContent;
        text.textColor = 0x000000;
        text.background = true;
        text.backgroundColor = 0x03f425;
        text.x = x;
        text.y = y;
        text.touchEnabled = true;
        text.addEventListener(egret.TouchEvent.TOUCH_TAP, callback, this);
        this.addChild(text);
    }

}